package com.aitaoxue.palx.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TextView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.widget.PagerSlidingTabStrip;

public class ViewPageFragmentAdapter extends FragmentPagerAdapter implements
		OnPageChangeListener {

	private static final String TAG = ViewPageFragmentAdapter.class
			.getSimpleName();

	public int REFRESH_STATE = NONE;

	public static final int NONE = 0;
	public static final int NOT_DEAL = 1;
	public static final int DEALING = 2;
	public static final int DEALED = 3;

	private final Context mContext;
	protected PagerSlidingTabStrip mPagerStrip;
	private final ViewPager mViewPager;
	private final ArrayList<ViewPagerInfo> mTabs = new ArrayList<ViewPagerInfo>();

	private FragmentManager fm;

	static class DummyTabFactory implements TabHost.TabContentFactory {

		private final Context mContext;

		public DummyTabFactory(Context context) {
			mContext = context;
		}

		@Override
		public View createTabContent(String arg0) {
			View v = new View(mContext);
			v.setMinimumWidth(0);
			v.setMinimumHeight(0);
			return v;
		}
	}

	public ViewPageFragmentAdapter(FragmentManager fm,
			PagerSlidingTabStrip pageStrip, ViewPager viewPager) {
		super(fm);
		this.fm = fm;
		mContext = viewPager.getContext();
		mPagerStrip = pageStrip;
		mViewPager = viewPager;
		mViewPager.setAdapter(this);
		mViewPager.setOnPageChangeListener(this);
		mPagerStrip.setViewPager(mViewPager);

	}

	public void addTab(String title, String tag, Class<?> clss, Bundle args) {
		ViewPagerInfo info = new ViewPagerInfo(title, tag, clss, args);
		mTabs.add(info);
	}

	@Override
	public void notifyDataSetChanged() {
		if (REFRESH_STATE == NONE) {
			for (ViewPagerInfo viewPageInfo : mTabs) {
				TextView v = (TextView) LayoutInflater.from(mContext).inflate(
						R.layout.base_viewpage_fragment_tab_item, null);
				v.setText(viewPageInfo.title);
				mPagerStrip.addTab(v);
			}
		}

		super.notifyDataSetChanged();

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public Fragment getItem(int arg0) {
		Log.d(TAG, "execute getitem");
		ViewPagerInfo info = mTabs.get(arg0);
		return Fragment.instantiate(mContext, info.clss.getName(), info.args);
	}

	@Override
	public int getCount() {
		return mTabs.size();
	}

	// @Override
	// public int getItemPosition(Object object) {
	// return super.getItemPosition(object);
	// }

	public void changeRefreshFlag(int flag) {
		this.REFRESH_STATE = flag;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		Fragment fragment = (Fragment) super.instantiateItem(container,
				position);

		String fragmentTag = fragment.getTag();

		if ((position == 0 && REFRESH_STATE == NOT_DEAL)
				|| (position == 1 && REFRESH_STATE == DEALING)
				|| (position == 2 && REFRESH_STATE == DEALED)
				|| (position == 1 && REFRESH_STATE == DEALED)) {
			// 刷新
			FragmentTransaction ft = fm.beginTransaction();
			ft.remove(fragment);
			ViewPagerInfo info = mTabs.get(position);
			fragment = Fragment.instantiate(mContext, info.clss.getName(),
					info.args);
			ft.add(container.getId(), fragment, fragmentTag);
			ft.attach(fragment);
			ft.commit();

			REFRESH_STATE = NONE;
		}

		return fragment;
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

}
